package com.stedu.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.stedu.bean.Admin;
import com.stedu.exception.SteduException;
import com.stedu.mapper.AdminMapper;
import com.stedu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public Admin login(String username, String password) throws SteduException {
        Admin admin = adminMapper.selectByUsername(username);
        //对用户输入的密码进行二次加密
        if (admin == null) {
            throw new SteduException("用户名输入错误");
        }
        //对用户输入的密码进行加密
            String md5Pwd = SecureUtil.md5(SecureUtil.md5(password));
            if(!md5Pwd.equals(admin.getPassword())){
                throw new SteduException("密码错误，请确认后重新登录");
            }
        return admin;
        }


    @Override
    public List<Map<String, Object>> selectTop5DnameAndCount() {
        return adminMapper.selectTop5DnameAndCount();
    }

    @Override
    public Admin selectByUsername(String username) {

        return adminMapper.selectByUsername(username);
    }

    @Override
    public boolean update(Admin admin) throws SteduException {

            return adminMapper.update(admin) == 1;

    }

    @Override
    public boolean Aupdate(Admin admin) throws SteduException {
        return adminMapper.Aupdate(admin) == 1;
    }

    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectById(id);
    }

    @Override
    public List<Map<String, Object>> selectTop5RnameAndCount() {
        return adminMapper.selectTop5RnameAndCount();
    }

    @Override
    public boolean insertAdmin(Admin a) {

        return adminMapper.insertAdmin(a);
    }
    @Override
    public boolean checkUsernameExist(Admin admin) {
        Integer count = adminMapper.checkUsernameExist(admin);
        return count > 0;
    }

    @Override
    public boolean deleteByRid(Integer rid) {
        return adminMapper.deleteByRid(rid);

    }
}
